<template>
  <div id="addAccount">
    <table>
      <tr>
        <td><span style="color: red">*</span>账户编号</td>
        <td><el-input v-model="account.accountCode"></el-input></td>
        <td><span style="color: red">*</span>账户名称</td>
        <td><el-input v-model="account.accountName"></el-input></td>
      </tr>
      <br />
      <tr>
        <td><span style="color: red">*</span>余额日期</td>
        <td>
          <el-date-picker
            v-model="account.accountDate"
            type="date"
            placeholder="选择日期"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </td>
        <td><span style="color: red">*</span>余额</td>
        <td><el-input v-model="account.presentMoney"></el-input></td>
      </tr>
      <br />
      <tr>
        <td><span style="color: red">*</span>账户类别</td>
        <td>
          <el-select v-model="account.aTypeId">
            <el-option
              v-for="accountType in accountTypes"
              :key="accountType.aTypeId"
              :label="accountType.aTypeName"
              :value="accountType.aTypeId"
            >
            </el-option>
          </el-select>
        </td>
        <td>设为默认</td>
        <td>
          <el-checkbox v-model="checked"></el-checkbox>
        </td>
      </tr>
      <br />
    </table>
  </div>
</template>

<script>
export default {
  name: "AddaccountType",
  data() {
    return {
      checked: false,
      account: {
        accountId:'',
        accountCode: "",
        accountName: "",
        presentMoney: "",
        accountDate: "",
        aTypeId: "",
      },
      accountTypes: [],
      oldAcount:[],
      isVaName:false,
      isVaCode:false,
    };
  },
  methods: {
    letNull() {
      for (const key in this.account) {
        if (this.account.hasOwnProperty(key)) {
          this.account[key] = "";
        }
      }
      this.oldAcount = [];
      this.checked=false
    },
    addAndUp: async function () {
      const accountName = this.account.accountName;
      const accountCode = this.account.accountCode;
      const aTypeId = this.account.aTypeId;
      const accountDate = this.account.accountDate;
      const presentMoney = this.account.presentMoney;
      let isDefault=0
      if(this.checked){
        isDefault =1
      }

      const accountId = this.account.accountId;

      if (!accountCode) {
        this.$message({
          showClose: true,
          message: "账户编码不可以为空",
          type: "error",
        });
        return false;
      }

      await this.vaCode();

      if (accountId && accountCode == this.oldAcount.accountCode) {
      } else if (this.isVaCode) {
        this.$message({
          showClose: true,
          message: "账户编码重复",
          type: "error",
        });
        return false;
      }

      if (!accountName) {
        this.$message({
          showClose: true,
          message: "账户名称不可以为空",
          type: "error",
        });
        return false;
      }

      await this.vaName();

      if (accountId && accountName == this.oldAcount.accountName) {
      } else if (this.isVaName) {
        this.$message({
          showClose: true,
          message: "账户名称重复",
          type: "error",
        });
        return false;
      }

      const regex = /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/;
      if (!regex.test(presentMoney)) {
        this.$message({
          showClose: true,
          message: "小数点后最多两位的正数",
          type: "error",
        });

        return false;
      }
      if(!aTypeId){
        this.$message({
          showClose: true,
          message: "账户类别不可以为空",
          type: "error",
        });
        return false;
      }

      if(!accountDate){
        this.$message({
          showClose: true,
          message: "账户时间不可以为空",
          type: "error",
        });
        return false;
      }

      console.log(this.account)

      console.log(accountDate)

      if (!accountId) {
        try {
          const rs = await this.axios({
            url: "/info/account/insert",
            method: "POST",
            data: {
              accountCode,
              accountName,
              aTypeId,
              presentMoney,
              accountDate,
              isDefault,
            },
          });
          if (rs.data.code === 200) {
            this.$message({
              showClose: true,
              message: "添加成功",
              type: "success",
            });

            this.letNull();
            return true;
          } else {
            this.$message({
              showClose: true,
              message: "添加失败",
              type: "error",
            });

            return false;
          }
        } catch (error) {
          this.$message({
            showClose: true,
            message: "添加失败,请求失败",
            type: "error",
          });
          return false;
        }
      } else {
        try {
          const rs = await this.axios({
            url: "/info/account/update",
            method: "POST",
            data: {
              accountCode,
              accountName,
              aTypeId,
              presentMoney,
              accountDate,
              isDefault,
              accountId,
            },
          });

          if (rs.data.code === 200) {
            this.$message({
              showClose: true,
              message: "修改成功",
              type: "success",
            });
            this.letNull();

            return true;
          } else {
            this.$message({
              showClose: true,
              message: "修改失败",
              type: "error",
            });

            return false;
          }
        } catch (error) {
          console.error("请求失败:", error);
          return false;
        }
      }
    },
    selectById(id) {
      this.axios({
        url: "/info/account/selectById",
        params: {
          accountId: id,
        },
      }).then((rs) => {
        this.oldAcount = rs.data.account;
        this.account = rs.data.account;
      });
    },
    vaCode: async function () {
      await this.axios({
        url: "/info/account/count",
        params: {
          accountCode: this.account.accountCode,
        },
      }).then((rs) => {
        if (rs.data.count > 0) {
          this.isVaCode = true;
        } else {
          this.isVaCode = false;
        }
      });
    },
    vaName: async function () {
      await this.axios({
        url: "/info/account/count",
        params: {
          accountName: this.account.accountName,
        },
      }).then((rs) => {
        if (rs.data.count > 0) {
          this.isVaName = true;
        } else {
          this.isVaName = false;
        }
      });
    },
    selectType() {
      this.axios({
        url: "/info/accountType/selectAllAccountTypes",
      }).then((rs) => {
        this.accountTypes = rs.data.accountTypes;
      });
    },
  },
  created() {
    this.selectType();
  },
};
</script>

<style scoped>
#addAccount {
  width: 100%;
  height: 200px;
  /* overflow: scroll; */
  /* background-color: pink; */
}

#contact {
  width: 90%;
  margin: auto;
  border-collapse: collapse;
  color: #707276;
}

#contact th,
#contact td {
  border: 1px #c0c4cc solid;
  height: 35px;
  min-width: 30px;
}

p {
  color: #4b4a4a;
  font-size: 18px;
}

.el-input {
  width: 240px;
}

table td :not([id="contact"]) {
  text-align: left;
  padding-right: 10px;
  /* padding-top: 10px;
  padding-bottom: 10px; */
}

td:nth-child(odd) {
  text-align: center;
  padding-right: 10px;
}
</style>